<?php

namespace Index\Controller;

use Think\Controller;
use Index\ViewModel;
use Index\Service;

/**
 * 说明：评论标签
 * 作者：菜菜
 * 创建日期：2017-09-15
 * comment/index.shtml
 */
class CommentController extends CommonController
{
    
    //获取列表
    public function index()
    {
        $map = [];
        $viewModel = M('chat_comment_tag');
        $total = $viewModel->where($map)->count();
        $row = '10';
        $page = new Service\PageService($total, $row);
        $fpage = $page->fpage();

        $data = $viewModel->where($map)->limit($page->listfirst, $page->listRows)->order('ctime desc')->select();
       
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'total' => $total,
            'apparr' => $this->getapplist()
        ));
        $this->display();
    }

    /**
     * 添加和修改
     */
    public function add()
    {
        if (IS_POST) { //修改 新增
            $id = I('id');
            $insert = [];
            $insert = [
                'name'=>I('name'),       
            ];

            $viewModel = M('chat_comment_tag');
            if (!empty($id)){
                $viewModel->where(['id'=>$id])->save($insert);
            }else {

                $viewModel->add($insert);

            }
            $response = array('ret' => '1001', 'code' => '操作成功！');

            $this->ajaxReturn($response, 'json');
    }

        $id = I('id');
        $viewModel = M('chat_comment_tag');
        if ($id) { //修改
            $data = $viewModel->where(['id'=>$id])->find();

            $this->assign(array(
                'data' => $data,
                'id' => $id,

            ));

        }
        $this->assign(array(

            'btnname'=>empty($id)?"添加":'修改',
        ));
        $this->display();

    }

    /**
     * 删除
     */
    public function delete(){
        $id = I('id');
        if (!empty($id)) {
            $model = M('chat_comment_tag');
            $result = $model->where(array('id' => $id))->delete();
            if ($result) {
                return $this->ajaxReturn(array('ret' => '1001', 'code' => '删除成功！'));
            } else {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '删除失败！'));
            }
        }
    }
}
